//-----------------------------------------------------------------------
// <copyright file="CommentDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CommentDAOBase
    {
        #region Common methods
        public virtual Comment CreateCommentFromReader(IDataReader reader)
        {
            Comment item = new Comment();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CommentId"))) item.CommentId = (long)reader["CommentId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("MemberId"))) item.MemberId = (long)reader["MemberId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("TGKM_ObjectId"))) item.TGKM_ObjectId = (long)reader["TGKM_ObjectId"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusId"))) item.StatusId = (int)reader["StatusId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCommentFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateComment methods
            
        public virtual void CreateComment(Comment comment)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCommentInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, comment.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, comment.Description);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, comment.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, comment.UpdatedDate);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, comment.MemberId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, comment.Order);
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, comment.TGKM_ObjectId);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, comment.StatusId);
                database.AddOutParameter(dbCommand, "@CommentId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                comment.CommentId = (long)database.GetParameterValue(dbCommand, "@CommentId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCommentException, ex);
            }
        }

        #endregion

        #region UpdateComment methods
        
        public virtual void UpdateComment(Comment comment)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCommentUpdate");            
                
                database.AddInParameter(dbCommand, "@CommentId", DbType.Int64, comment.CommentId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, comment.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, comment.Description);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, comment.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, comment.UpdatedDate);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, comment.MemberId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, comment.Order);
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, comment.TGKM_ObjectId);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, comment.StatusId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCommentException, ex);
            }
        }
        
        #endregion

        #region DeleteComment methods
        public virtual void DeleteComment(long commentId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCommentDelete");
                
                database.AddInParameter(dbCommand, "@CommentId", DbType.Int64, commentId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCommentException, ex);
            }
        }

        #endregion

        #region GetComment methods
        
        public virtual Comment GetComment(long commentId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCommentGet");
                
                database.AddInParameter(dbCommand, "@CommentId", DbType.Int64, commentId);
                
                Comment comment =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        comment = CreateCommentFromReader(reader);
                        reader.Close();
                    }
                }
                return comment;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCommentException, ex);
            }
        }
        
        #endregion

        #region GetCommentList methods
        public virtual CommentCollection GetCommentList(CommentColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCommentGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CommentCollection commentCollection = new CommentCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Comment comment = CreateCommentFromReader(reader);
                        commentCollection.Add(comment);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return commentCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCommentListException, ex);
            }
        }
        
        public virtual CommentCollection GetCommentList(CommentColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCommentList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCommentList methods
        public virtual CommentCollection GetCommentListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCommentGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CommentCollection commentCollection = new CommentCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Comment comment = CreateCommentFromReader(reader);
                        commentCollection.Add(comment);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return commentCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCommentListException, ex);
            }
        }
        
        public virtual CommentCollection GetCommentListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCommentListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}